<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Drug;

/**
 * DrugSearch represents the model behind the search form of `common\models\Drug`.
 */
class DrugSearch extends Drug
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status'], 'integer'],
            [['barcode', 'name', 'name_intro', 'spell', 'spell_intro', 'specification', 'unit', 'shape', 'vender', 'employ', 'action', 'coding'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Drug::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
        ]);

        $query->andFilterWhere(['like', 'barcode', $this->barcode])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'name_intro', $this->name_intro])
            ->andFilterWhere(['like', 'spell', $this->spell])
            ->andFilterWhere(['like', 'spell_intro', $this->spell_intro])
            ->andFilterWhere(['like', 'specification', $this->specification])
            ->andFilterWhere(['like', 'unit', $this->unit])
            ->andFilterWhere(['like', 'shape', $this->shape])
            ->andFilterWhere(['like', 'vender', $this->vender])
            ->andFilterWhere(['like', 'employ', $this->employ])
            ->andFilterWhere(['like', 'action', $this->action])
            ->andFilterWhere(['like', 'coding', $this->coding]);

        return $dataProvider;
    }
}
